package org.example.springaop.interceptor;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class CGLIBInterceptor implements MethodInterceptor {
    // 创建目标对象
    private Object target;
    public CGLIBInterceptor(Object target) {
        this.target = target;
    }
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        // 代理增强内容
        System.out.println("我是中介，开始代理");
        // 通过反射调用被代理类的方法
        Object result = methodProxy.invoke(target, objects);
        // 代理增强内容
        System.out.println("我是中介，结束代理");
        return result;
    }
}
